package product;


import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.LinkedHashMap;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class ProductMain extends JFrame{

	HashMap<String, MemberData> mem = new HashMap<>();
	HashMap<String, TempData> chul = new HashMap<>();
	LinkedHashMap<String, ChulGData> chulG = new LinkedHashMap<>();
	HashMap<String, MemberData> holy = new HashMap<>();

	HashMap<String, ProductData> pd = new HashMap<String, ProductData>();
	
	
	String driver="oracle.jdbc.driver.OracleDriver";
	String db_user ="hr";
	String db_pwd="hr";
	Connection conn;
	
	ProductMain pm=this;
	CodeInput cInput = null;
	CodeUpdate cUpdate = null;
	ProductInput pInput = null;
	ProductUpdate pUpdate = null;
	MemberInput mInput = null;
	MemberUpdate mUpdate = null;
	semiChul semiC = null;
	MemberHoly mHoly = null;
	MemberSelect Mselect = null;
	MemberHolyselect mhSelect = null;

	MemberThread mt;

	ObjectInputStream ois;


	static String filename = "Untitled";
	boolean modifyFlag = false;

	JPanel contentPane;
	private JMenuBar menuBar;

	int yn;
	private JMenu mnMember;
	private JMenuItem mntmMemberinput;
	private JMenuItem mntmMemberupdate;
	private JMenuItem mntmMemberinout;
	private JMenuItem mntmMemberholy;
	private JMenuItem mntmNewMenuItem;


	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProductMain frame = new ProductMain();
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
					SwingUtilities.updateComponentTreeUI(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	public ProductMain() {
		setTitle("Product Manage Program");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1002, 693);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();		
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		ipchange();
		

		mt = new MemberThread(pm);

	}
	
	
	public void ipchange(){
//		데이터베이스 연결 테스트
//		1)드라이버로딩
		try{
			InetAddress ia = InetAddress.getLocalHost();
			String a = JOptionPane.showInputDialog(null, "아이피주소를 입력해주세요",ia.getHostAddress());
			String url="jdbc:oracle:thin:@"+a+":1521:xe";
			if(a!=null){
				Class.forName(driver).newInstance();
				System.out.println("드라이버가 로딩되었습니다.");
				
			//2)Connection 객체 생성
				conn=DriverManager.getConnection(url, db_user,db_pwd);
				System.out.println("데이터베이스가 정상적으로 연결되었습니다.");	
				
			}
			a=null;
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void openselect(){
		if(Mselect == null){
			Mselect= new MemberSelect(conn,this);
			Mselect.setBounds(0, 0, 1000, 300);	
			contentPane.add(Mselect);
			Mselect.columnadd();
		}
		
		Mselect.setVisible(true);
		Mselect.toFront();
	}
	
	public void openholyselect(){
		if(mhSelect == null){
			mhSelect = new MemberHolyselect(conn,this);
			mHoly.setH(mhSelect);
			mhSelect.setBounds(0, 0,  450, 300);
			contentPane.add(mhSelect);
			mhSelect.columnadd();
		}
		mhSelect.setVisible(true);
		mhSelect.toFront();
	}
	
	public void openinput(){
		if(mInput == null){
			mInput = new MemberInput(conn,this);	
			mInput.setD(Mselect);
			mInput.setBounds(480, 300, 430, 250);		
			contentPane.add(mInput);
		}
		mInput.toFront();
		mInput.setVisible(true);

	}
	
	
	public void openupdate(){
		if(mUpdate==null){
			mUpdate = new MemberUpdate(conn,this);
			Mselect.setD(mUpdate);
			mUpdate.setU(Mselect);
			mUpdate.setBounds(0, 300, 400, 400);
			contentPane.add(mUpdate);
		}

		mUpdate.setVisible(true);
		mUpdate.toFront();
	}
	


	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnMember());
		}
		return menuBar;
	}

	public void saveP() throws Exception{
		if(filename.equals("Untitled")){ // 파일명이 정해지지 않았을때
			saveAsP();
		}
		if(yn == JFileChooser.CANCEL_OPTION){
			JOptionPane.showMessageDialog(null, "Save is Canceled");
		}else{
			FileOutputStream fos = new FileOutputStream(filename);
			ObjectOutputStream oos = new ObjectOutputStream(fos);

			oos.writeObject(pd);
			JOptionPane.showMessageDialog(null, "Your File is Safely Saved.");		
			oos.close();
			fos.close();
			setTitle("Filename : " + filename);
			modifyFlag = false;
		}		
	}
	public void saveAsP() throws Exception{
		JFileChooser fc = new JFileChooser(filename);
		yn = fc.showSaveDialog(this);
		if(yn == JFileChooser.APPROVE_OPTION){
			File f = fc.getSelectedFile();
			filename = f.getPath();
			saveP();
		}
	}
	private JMenu getMnMember() {
		if (mnMember == null) {
			mnMember = new JMenu("Member");
			mnMember.add(getMntmMemberinput());
			mnMember.add(getMntmMemberupdate());
			mnMember.add(getMntmNewMenuItem());
			mnMember.add(getMntmMemberinout());
			mnMember.add(getMntmMemberholy());
		}
		return mnMember;
	}
	private JMenuItem getMntmMemberinput() {
		if (mntmMemberinput == null) {
			mntmMemberinput = new JMenuItem("MemberInput");
			mntmMemberinput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				
					openinput();

				}
			});
		}
		return mntmMemberinput;
	}
	private JMenuItem getMntmMemberupdate() {
		if (mntmMemberupdate == null) {
			mntmMemberupdate = new JMenuItem("MemberUpdate");
			mntmMemberupdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(mUpdate == null){
						mUpdate = new MemberUpdate(conn,ProductMain.this);
//						mUpdate.setI(mInput);
//						mInput.setD(mUpdate);
						mUpdate.setBounds(50, 300, 450, 320);	
						contentPane.add(mUpdate);
					}
					
					mUpdate.setVisible(true);
					mUpdate.toFront();
				}
			});
		}
		return mntmMemberupdate;
	}
	private JMenuItem getMntmMemberinout() {
		if (mntmMemberinout == null) {
			mntmMemberinout = new JMenuItem("MemberIn&Out");
			mntmMemberinout.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(semiC == null){
						semiC = new semiChul(mem,pm);						
						semiC.setBounds(500, 300, 450, 300);						
					}
					contentPane.add(semiC);
					semiC.setVisible(true);
				}
			});
		}
		return mntmMemberinout;
	}
	private JMenuItem getMntmMemberholy() {
		if (mntmMemberholy == null) {
			mntmMemberholy = new JMenuItem("MemberHoly");
			mntmMemberholy.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(mHoly == null){
						mHoly = new MemberHoly(conn,pm);						
						mHoly.setBounds(500, 300, 450, 300);						
					}
					contentPane.add(mHoly);
					mHoly.setVisible(true);
				}
			});
		}
		return mntmMemberholy;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("MemberSelect");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					openselect();
				}
			});
		}
		return mntmNewMenuItem;
	}
}
